package com.tuzhh.htmlrtspplayer.handler;

import com.tuzhh.htmlrtspplayer.bean.SessionInfo;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

@Component
public class DataHandler extends AbstractWebSocketHandler {

    private String protocal;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        HttpHeaders handshakeHeaders = session.getHandshakeHeaders();
        this.protocal = handshakeHeaders.getFirst("Sec-WebSocket-Protocol");
        this.protocal = "data";
        System.out.println(this.protocal + "已连接:" + session.getId());
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        Handler.handleTextMessage(protocal, session, message);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        SessionInfo sessionInfo = SessionInfo.get(session.getId());
        if (sessionInfo != null) {
            SessionInfo.remove(session.getId());
        }
        System.out.println(this.protocal + "已关闭:" + session.getId());
    }
}
